package Text1;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:墨琦
 * Data:2023-04-09
 * Time:11:44
 */
public class MyList {

    static class List{
        int data;
        List next;
        List(){}
        List(int  data){
            this.data=data;
        }
    }
    List head;

    public void insertHead(int  sum)
    {
        List fw=new List(sum);
        fw.next =head;
        head=fw;
    }
    public void insertTail(int sum)
    {
        List fw=new List(sum);
        if(head==null)
        {
            head=fw;
            return;
        }
        List fw1=head;
        while(fw1.next!=null)
        {
            fw1=fw1.next;
        }
        fw1.next=fw;
    }
    public int size()
    {
        List cur=new List();
        cur=head;
        int size=0;
        while(cur!=null)
        {
            cur=cur.next;
            size++;
        }
        return size;
    }
    public void show()
    {
        List cur=new List();
        cur=head;
        while(cur!=null)
        {
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
    }
    public void show(List kk)
    {
        List cur=kk;
        while(cur!=null)
        {
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
        System.out.println("");
    }
    //链表反转
    public void reversal()
    {
        List cur=new List();
        List curnext=new List();
        cur=head.next;
        head.next=null;
        curnext=cur.next;
        while(cur!=null)
        {
            cur.next=head;
            head=cur;
            cur=curnext;
            if(curnext!=null)
            {
                curnext=curnext.next;
            }

        }

    }
    //取中间节点
    public MyList.List centre(MyList.List KK)
    {
        if(KK.next==null)
        {
            return KK;
        }
        List cur=KK;
        List prv=KK;
       while(cur!=null&&cur.next!=null)
       {
           cur=cur.next.next;
           prv=prv.next;
       }
        return prv;
    }

    public MyList.List ReciprocalNode(MyList.List KK,int num)
    {
        if(num<=0||KK==null)
        {
            return null;
        }
        int size=0;
        MyList.List fw=KK;
        while(fw!=null)
        {
            size++;
            fw=fw.next;
        }
        MyList.List fw1=KK;
        while(size!=num)
        {
            fw1=fw1.next;
            size--;
        }
        return fw1;
    }
    public Boolean Palindrome(MyList.List KK)
    {
        if(KK.next==null)
        {
            return false;
        }
        List cur=KK;
        List prv=KK;
        while(cur!=null&&cur.next!=null)
        {
            cur=cur.next.next;
            prv=prv.next;
        }
        List cur1=prv.next;
        while(cur1!=null) {
           List curnext=cur1.next;
           cur1.next=prv;
           prv=cur1;
           cur1=curnext;

        }
        List left=KK;
        while(left.next!=prv&&left.next!=prv.next)
        {
            left=left.next;
            prv=prv.next;
            if(left.data!=prv.data)
            {
                return  false;
            }
        }
        return true;
    }
}
